toolbar = UI::Toolbar.new "Xpawn"
# 创建Fourline
cmd_fourline = UI::Command.new("Xpawn") {
	main_drawFourline()
}
cmd_fourline.small_icon = "ToolPencilSmall.png"
cmd_fourline.large_icon = "ToolPencilLarge.png"
cmd_fourline.status_bar_text = "根据一个面画出对应的四个边线"
cmd_fourline.tooltip = "Fourline"
cmd_fourline.menu_text = "Fourline"
toolbar = toolbar.add_item cmd_fourline
# 创建HorizontalWithBar
cmd_horizontal_with_bar = UI::Command.new("Xpawn") {
	main_horizontalWithBar()
}
cmd_horizontal_with_bar.small_icon = "ToolPencilSmall.png"
cmd_horizontal_with_bar.large_icon = "ToolPencilLarge.png"
cmd_horizontal_with_bar.status_bar_text = "用bar进行水平分割"
cmd_horizontal_with_bar.tooltip = "HorizontalWithBar"
cmd_horizontal_with_bar.menu_text = "HorizontalWithBar"
toolbar = toolbar.add_item cmd_horizontal_with_bar

toolbar.show


=begin
	当face垂直于x轴时，寻找LeftBottom
=end
def findFaceLeftBottom(points)
   point_result=points[0]
   points.each do |point|
    if (point.position.y+point.position.z)<(point_result.position.y+point_result.position.z) then
      point_result = point
    end
   end
   return point_result
end

=begin
	当face垂直于x轴时，寻找RightTop
=end
def findFaceRightTop(points)
   point_result=points[0]
   points.each do |point|
    if (point.position.y+point.position.z)>(point_result.position.y+point_result.position.z) then
      point_result = point
    end
   end
   return point_result
end

=begin
	当face垂直于x轴时，寻找LeftTop
=end
def findFaceLeftTop(points)
   leftbottom=findFaceLeftBottom(points)
   righttop=findFaceRightTop(points)
   points.each do |point|
    if (point.position.y==leftbottom.position.y)&&(point.position.z==righttop.position.z) then
      return point
    end
   end
end

=begin
	当face垂直于x轴时，寻找RightBottpm
=end
def findFaceRightBottpm(points)
   leftbottom=findFaceLeftBottom(points)
   righttop=findFaceRightTop(points)
   points.each do |point|
    if (point.position.y==righttop.position.y)&&(point.position.z==leftbottom.position.z) then
      return point
    end
   end
end

=begin
	检查面是否满足要求
=end
def checkIsFace(sel)
  selFaceEntity = sel.first
  if sel.count != 1 then
    UI.messagebox("选择的只能是一个面")
  return false
  end

  if "Sketchup::Face"!=selFaceEntity.class.to_s then
    UI.messagebox("选择的不是一个面")
    return false
  end

  selEdgesArray = selFaceEntity.edges
  if 4 != selEdgesArray.length then
    UI.messagebox("请确定选择的是一个四边形")
    return false
  end

  normal = selFaceEntity.normal
  if (normal.x == 0 || normal.y != 0 || normal.z != 0) then
    UI.messagebox("只能操作垂直于X轴的Face")
    return false
  end
  return true
end

=begin
	用bar进行水平分割
=end
def main_horizontalWithBar()
  model = Sketchup.active_model
  entities = model.entities
  sel = model.selection
  selFaceEntity = sel.first
  points = selFaceEntity.vertices
  p "----------------->start"
  if !checkIsFace(sel) then
  	return -1
  end
  leftbottom=findFaceLeftBottom(points).position
  righttop=findFaceRightTop(points).position
  lefttop=findFaceLeftTop(points).position
  rightbottom=findFaceRightBottpm(points).position

  p "leftbottom ： #{leftbottom}"
  p "lefttop ： #{lefttop}"
  p "righttop ： #{righttop}"
  p "rightbottom ： #{rightbottom}"

  height = (lefttop.z.to_mm - leftbottom.z.to_mm)
  p "leftbottom.z : #{leftbottom.z.to_mm}"
  p "lefttop.z : #{lefttop.z.to_mm}"
  p "height : #{height}"
  # With three params, it shows all text boxes:
  prompts = ["输入多少等分", "输入挡板厚度"]
  defaults = ["", "20"]
  input = UI.inputbox(prompts, defaults, "输入参数")
  divide = input[0].to_i
  p "divide : #{divide}"
  barheight = input[1].to_i
  boxheight = (height - (divide-1) * barheight) / divide
  p "boxheight : #{boxheight.mm}"
  for divideIndex in 1..(divide-1) do
    p "--------> : #{divideIndex}"
    zaddvalue1=(divideIndex*boxheight+(divideIndex-1)*barheight).mm
    zaddvalue2=(divideIndex*boxheight+divideIndex*barheight).mm
    leftpoint1 = [leftbottom.x,leftbottom.y,leftbottom.z+zaddvalue1]
    rightpoint1 = [rightbottom.x,rightbottom.y,rightbottom.z+zaddvalue1]
    leftpoint2 = [leftbottom.x,leftbottom.y,leftbottom.z+zaddvalue2]
    rightpoint2 = [rightbottom.x,rightbottom.y,rightbottom.z+zaddvalue2]
    p "--------> : #{leftpoint1}"
    p "--------> : #{rightpoint1}"
    p "--------> : #{leftpoint2}"
    p "--------> : #{rightpoint2}"
    entities.add_line [leftpoint1,rightpoint1]
    entities.add_line [leftpoint2,rightpoint2]
  end
  
  
  p "----------------->end"
end

=begin
	根据一个面画出对应的四个边线
=end
def main_drawFourline()
  model = Sketchup.active_model
  entities = model.entities
  sel = model.selection
  selFaceEntity = sel.first
  points = selFaceEntity.vertices

  p "----------------->start"
  if !checkIsFace(sel) then
  	return -1
  end
  
  leftbottom=findFaceLeftBottom(points).position
  righttop=findFaceRightTop(points).position
  lefttop=findFaceLeftTop(points).position
  rightbottom=findFaceRightBottpm(points).position


  p "leftbottom ： #{leftbottom}"
  p "lefttop ： #{lefttop}"
  p "righttop ： #{righttop}"
  p "rightbottom ： #{rightbottom}"

  # With three params, it shows all text boxes:
  prompts = ["输入两边距离", "输入上边距离", "输入下边距离"]
  defaults = ["60", "80", "80"]
  input = UI.inputbox(prompts, defaults, "输入参数")

  p "input : #{input}"

  p "input[0].to_i.mm #{input[0].to_i.mm}"

  r_leftline=[[leftbottom.x,leftbottom.y + input[0].to_i.mm,leftbottom.z],[lefttop.x,lefttop.y + input[0].to_i.mm,lefttop.z]]
  p "r_leftline ： #{r_leftline}"
  entities.add_line r_leftline

  r_rightline=[[lefttop.x,righttop.y - input[0].to_i.mm,lefttop.z],[righttop.x,righttop.y - input[0].to_i.mm,rightbottom.z]]
  p "r_rightline ： #{r_rightline}"
  entities.add_line r_rightline

  r_topline=[[lefttop.x,lefttop.y + input[0].to_i.mm,lefttop.z - input[1].to_i.mm],[righttop.x,righttop.y - input[0].to_i.mm,lefttop.z - input[1].to_i.mm]]
  p "r_topline ： #{r_topline}"
  entities.add_line r_topline

  r_bottomline=[[lefttop.x,lefttop.y + input[0].to_i.mm,leftbottom.z + input[2].to_i.mm],[righttop.x,righttop.y - input[0].to_i.mm,rightbottom.z + input[2].to_i.mm]]
  p "r_bottomline ： #{r_bottomline}"
  entities.add_line r_bottomline

  p "----------------->end"
end


